﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;

using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using TRPOP.Config;

namespace TRPOP.CMS.Web.Admin
{
    public partial class Node_control : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.Node_Save;
        }
        #endregion

        #region 页面变量
        protected int m_id = 0;
        protected NodeInfo m_NodeInfo = new NodeInfo();
        protected NodeManage m_NodeManage = new NodeManage();
        protected string m_strAction = "";
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            m_strAction = RequestHelper.RequestString("ac");
            m_id = RequestHelper.RequestInt("nodeid");
            if (m_id > 0)//验证用户对结点的权限
                base.AlertForNoPermission(base.CheckIsHavePermission(m_id));
            if (m_strAction.IndexOf("ajax") > -1)
            {
                AjaxMethod();
            }
        }

        #endregion

        #region 页面方法
        private void AjaxMethod()
        {
            JavaScriptObject result = new JavaScriptObject();
            int m_nodeId = 0;
            if (m_strAction == "ajax_del")
            {
                #region 逻辑删除结点
                m_nodeId = RequestHelper.RequestInt("id");
                if (m_nodeId > 0)
                {
                    EnumSubmitResult enmResult = m_NodeManage.DeleteNodeAndChildrenById(m_nodeId);
                    base.SaveLog(Plugin.LogKeys.OperObject.NodeOper, Plugin.LogKeys.OperResult.DelSubmit);
                    switch (enmResult)
                    {
                        case EnumSubmitResult.Success:
                            result.Add("result", "1");
                            result.Add("msg", "删除成功");
                            break;
                        case EnumSubmitResult.Failed:
                            result.Add("result", "0");
                            result.Add("msg", "删除失败");
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    result.Add("result", "-1");
                    result.Add("msg", "参数有误");
                }
                #endregion
            }
            else if (m_strAction == "ajax_destroy")
            {
                #region 永久删除结点
                m_nodeId = RequestHelper.RequestInt("nodeid");
                bool isDelFile=RequestHelper.RequestInt("delfile")==0?false:true;
                bool isDelAttr = RequestHelper.RequestInt("delattr") == 0 ? false : true;
                bool isDelImg = RequestHelper.RequestInt("delimg") == 0 ? false : true;//删除图片
                if (m_nodeId > 0)
                {
                    EnumSubmitResult enmResult = m_NodeManage.DestroyNodeAndContetnById(m_nodeId, isDelFile, isDelAttr, isDelImg);
                    base.SaveLog(Plugin.LogKeys.OperObject.NodeOper, Plugin.LogKeys.OperResult.NodeDestroy);
                    switch (enmResult)
                    {
                        case EnumSubmitResult.Success:
                            result.Add("result", "1");
                            result.Add("msg", "删除成功");
                            break;
                        case EnumSubmitResult.Failed:
                            result.Add("result", "0");
                            result.Add("msg", "删除失败");
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    result.Add("result", "-1");
                    result.Add("msg", "参数有误");
                }
                #endregion
            }
            else if (m_strAction == "ajax_restore")
            {
                #region 恢复结点
                m_nodeId = RequestHelper.RequestInt("restorenodeid");
                if (m_nodeId > 0)
                {
                    m_NodeInfo = m_NodeManage.GetInfoById(m_nodeId);
                    if (m_NodeInfo != null)
                    {
                        m_NodeInfo.ParentId = m_id;
                        m_NodeInfo.IsHidde = false;
                        EnumSubmitResult enmResult = m_NodeManage.Save(m_NodeInfo);
                        base.SaveLog(Plugin.LogKeys.OperObject.NodeOper, Plugin.LogKeys.OperResult.NodeRestore);
                        switch (enmResult)
                        {
                            case EnumSubmitResult.Success:
                                result.Add("result", "1");
                                result.Add("msg", "恢复成功");
                                break;
                            case EnumSubmitResult.Failed:
                                result.Add("result", "0");
                                result.Add("msg", "恢复失败");
                                break;
                            default:
                                result.Add("result", "-1");
                                result.Add("msg", "恢复失败");
                                break;
                        }
                    }
                    else
                    {
                        result.Add("result", "-1");
                        result.Add("msg", "恢复失败,原因结点已经不存在了");
                    }
                }
                else
                {
                    result.Add("result", "-1");
                    result.Add("msg", "恢复失败,恢复的结点Id是0");
                }
                #endregion
            }
            else if (m_strAction == "ajax_empty_recycle_bin")
            {
                #region 清空结点回收站
                bool isDelFile = RequestHelper.RequestInt("delfile") == 0 ? false : true;
                bool isDelAttr = RequestHelper.RequestInt("delattr") == 0 ? false : true;
                bool isDelImg = RequestHelper.RequestInt("delimg") == 0 ? false : true;
                try
                {
                    List<NodeInfo> listNodeInfo = m_NodeManage.GetListByCondition(0, " and IsHidde=1", " NodeId asc");
                    foreach (NodeInfo info in listNodeInfo)
                    {
                        m_NodeManage.DestroyNodeAndContetnById(info.NodeId, isDelFile, isDelAttr, isDelImg);
                    }
                    base.SaveLog(Plugin.LogKeys.OperObject.NodeOper, Plugin.LogKeys.OperResult.NodeRecycle);
                    result.Add("result", "1");
                    result.Add("msg", "清空回收站成功");
                }
                catch
                {
                    result.Add("result", "0");
                    result.Add("msg", "清空回收站失败");
                }
                #endregion
            }
            else if (m_strAction == "ajax_set_sortvalue")
            {
                #region 结点排序
                m_nodeId = RequestHelper.RequestInt("nodeid");
                int intSortVal = RequestHelper.RequestInt("sortval");
                if (m_nodeId > 0)
                {
                    m_NodeInfo = m_NodeManage.GetInfoById(m_nodeId);
                    if (m_NodeInfo != null)
                    {
                        m_NodeInfo.DisplayOrder = intSortVal;
                        EnumSubmitResult enmResult = m_NodeManage.Save(m_NodeInfo);
                        base.SaveLog(Plugin.LogKeys.OperObject.NodeOper, Plugin.LogKeys.OperResult.NodeSort);
                        switch (enmResult)
                        {
                            case EnumSubmitResult.Success:
                                result.Add("result", "1");
                                result.Add("msg", "操作成功");
                                break;
                            case EnumSubmitResult.Failed:
                                result.Add("result", "0");
                                result.Add("msg", "操作失败");
                                break;
                            default:
                                result.Add("result", "-1");
                                result.Add("msg", "操作失败");
                                break;
                        }
                    }
                    else
                    {
                        result.Add("result", "-1");
                        result.Add("msg", "操作失败,原因结点已经不存在了");
                    }
                }
                else
                {
                    result.Add("result", "-1");
                    result.Add("msg", "操作失败,操作的结点Id是0");
                }
                #endregion
            }
            else
            {
                result.Add("result", "1");
                result.Add("msg", "ok");
            }
            Response.Write(JavaScriptConvert.SerializeObject(result));
            base.End();
        }
        #endregion

    }
}
